package com.ruoyi.web.controller.drugSystem.drugInfo;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drugSystem.domain.SysDrugClass;
import com.ruoyi.drugSystem.service.ISysDrugClassService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author 努力的派大星
 * @version 1.0
 */
@Api("药品分类信息管理")
@RestController
@RequestMapping("/drugInfo/drugClass")
public class SysDrugClassController extends BaseController{
    @Autowired
    private ISysDrugClassService drugClassService;

    /**
     * 获取药品种类列表
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:list')")
    @GetMapping("/list")
    @ApiOperation("药品种类列表")
    public TableDataInfo list(SysDrugClass drugClass)
    {
        startPage();
        List<SysDrugClass> list = drugClassService.selectDrugClassList(drugClass);
        return getDataTable(list);
    }

    @Log(title = "药品种类管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDrugClass drugClass)
    {
        List<SysDrugClass> list = drugClassService.selectDrugClassList(drugClass);
        ExcelUtil<SysDrugClass> util = new ExcelUtil<SysDrugClass>(SysDrugClass.class);
        util.exportExcel(response, list, "药品种类数据");
    }

    /**
     * 根据药品种类编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:query')")
    @GetMapping(value = "/{drugClassId}")
    public AjaxResult getInfo(@PathVariable Long drugClassId)
    {
        return success(drugClassService.selectDrugClassById(drugClassId));
    }

    /**
     * 新增药品种类
     */
    @ApiOperation("增加药品种类")
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:add')")
    @Log(title = "药品种类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysDrugClass drugClass)
    {
        if (!drugClassService.checkDrugClassNameUnique(drugClass))
        {
            return error("新增药品种类'" + drugClass.getDrugClassName() + "'失败，药品种类名称已存在");
        }
        else if (!drugClassService.checkDrugClassCodeUnique(drugClass))
        {
            return error("新增药品种类'" + drugClass.getDrugClassName() + "'失败，药品种类编码已存在");
        }
        drugClass.setCreateBy(getUsername());
        return toAjax(drugClassService.insertDrugClass(drugClass));
    }

    /**
     * 修改药品种类
     */
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:edit')")
    @Log(title = "药品种类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改药品种类")
    public AjaxResult edit(@Validated @RequestBody SysDrugClass drugClass)
    {
        if (!drugClassService.checkDrugClassNameUnique(drugClass))
        {
            return error("修改药品种类'" + drugClass.getDrugClassName() + "'失败，药品种类名称已存在");
        }
        else if (!drugClassService.checkDrugClassCodeUnique(drugClass))
        {
            return error("修改药品种类'" + drugClass.getDrugClassName() + "'失败，药品种类编码已存在");
        }
        drugClass.setUpdateBy(getUsername());
        return toAjax(drugClassService.updateDrugClass(drugClass));
    }

    /**
     * 删除药品种类
     */
    @ApiOperation("删除药品")
    @PreAuthorize("@ss.hasPermi('drugInfo:drugClass:remove')")
    @Log(title = "药品种类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugClassIds}")
    public AjaxResult remove(@PathVariable Long[] drugClassIds)
    {
        return toAjax(drugClassService.deleteDrugClassByIds(drugClassIds));
    }

    /**
     * 获取药品种类选择框列表
     */
    @GetMapping("/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDrugClass> drugClasss = drugClassService.selectDrugClassAll();
        return success(drugClasss);
    }
}
